package main.niit.servelet;


import com.alibaba.fastjson.JSONObject;
import main.niit.dao.mysqlDAO;
import main.niit.pojo.bean;
import main.niit.until.mysqlUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/servlet5")
public class servlet5 extends HttpServlet {
    Connection connection=null;
    Statement statement=null;
    ResultSet resultSet=null;
    @Override
    public void init() throws ServletException {
        try {
            connection = mysqlUtil.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<bean> bean1 = new ArrayList<>();
        mysqlDAO dao = new mysqlDAO();
        String name = req.getParameter("name");
        bean1=dao.getvalue5(connection, statement, resultSet,name);
        String data = JSONObject.toJSONString(bean1);
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(data);
        System.out.println(bean1);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override

    public void destroy() {
        mysqlUtil.close(connection,statement,resultSet);
    }
}
